<?php
namespace app\api\command;

use redis\JkRedis;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class EntrustEvent extends Command
{
    protected function configure()
    {
        $this->setName('entrust')
        	 ->setDescription('stock_entrust_event');
    }

    protected function execute(Input $input, Output $output)
    {
        $redis = new JkRedis();
        $redis->setOption();

        $redis->psubscribe(array('__keyevent@0__:expired'), function ($redis, $pattern, $channel, $msg){
            if( preg_match('/entrust:id:(\d+)/',$msg,$matches) ){
                $pkid = $matches[1];
                $rows = Db::name('share_entrust')
                    ->where('id',$pkid)
                    ->update([
                        'status' => 0,
                        'updatetime' => time()
                    ]);
                Log::write("$msg | update $rows",'observe');
            }
        });
    }
}